/**
 * FileName: SFTPController
 * Author:   Leopold
 * Date:     2018/10/4 9:01
 * Description:
 * History:
 */
package cn.mcsys.platform.core.web;

/**
 * FileName: SFTPCtroller
 * Author:   Leopold
 * Date:     2018/10/2 17:09
 * Description:
 * History:
 */

import cn.mcsys.myadmin.common.utils.SFTPUtils;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.mobile.entity.AirRealtimeUserCount;
import cn.mcsys.myadmin.modules.mobile.entity.AirUserFlowCount;
import com.jcraft.jsch.SftpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Controller
public class SFTPController {

    @Autowired
    private BackService backService;

    @RequestMapping("sftp")
    @ResponseBody
    public JsonResult sftp() throws IOException, SftpException, ParseException {
//        获取本地的SFTP文件
//        列出本地的保存sftp文件的目录
        File dir = new File("D:\\airshow\\sftp");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File[] files = dir.listFiles();
        List<String> mySFTPFiles = new ArrayList<>();
        for (File file : files) {
            mySFTPFiles.add(file.getName());
        }
        SFTPUtils sftp = new SFTPUtils("ZhHumanFlow", "Yidong@10086", "14.29.68.61", 30123);
        sftp.login();
        Vector<?> ls = sftp.listFiles("/home/sftp");
        List<String> newSFTPFile = new ArrayList<>();
//        下载sftp文件
        for (Object sftpFile : ls) {
            String string = sftpFile.toString();//对象toString
            if (string.endsWith("txt"))//如果是以txt结尾，那就进入
            {
                //根据规律文件名切割字符串
                String substring = string.substring(string.lastIndexOf(" "), string.length());
                String sftpFileName = substring.trim();
                System.out.println(sftpFileName);
//                如果本地不包含该文件就下载该文件并解析
                if (!mySFTPFiles.contains(substring.trim())) {
                    String dirSFTPFile = dir + "\\" + sftpFileName;
                    newSFTPFile.add(dirSFTPFile);
//                    要具体到文件中
                    sftp.download("/home/sftp", sftpFileName, dirSFTPFile);
                    System.out.println(sftpFileName + "-------下载完成");
                }
            }
        }

//        读入sftp文件
        List<AirRealtimeUserCount> airRealtimeUserCounts = new ArrayList<>();
        List<AirUserFlowCount> airUserFlowCounts = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (String sftpFile : newSFTPFile) {
            File file = new File(sftpFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String sLine = null;
            if(sftpFile.contains("user_flow_count")){
                while ((sLine = br.readLine()) != null) {
                    sLine = sLine.trim();
                    System.out.println("sline:" + sLine);
                    String[] ary = sLine.split("\\|");
                    AirUserFlowCount airUserFlowCount = new AirUserFlowCount();
                    airUserFlowCount.setStartDateTime(format.parse(ary[0]));
                    airUserFlowCount.setAreaId(Integer.valueOf(ary[1]));
                    airUserFlowCount.setUserInflowCount(Long.parseLong(ary[2]));
                    airUserFlowCount.setUserOutflowCount(Long.parseLong(ary[3]));
                    airUserFlowCounts.add(airUserFlowCount);
                }
            }else if(sftpFile.contains("realtime_user_count")){
                while ((sLine = br.readLine()) != null) {
                    sLine = sLine.trim();
                    System.out.println("sline:" + sLine);
                    String[] ary = sLine.split("\\|");
                    AirRealtimeUserCount airRealtimeUserCount = new AirRealtimeUserCount();
                    airRealtimeUserCount.setStartDateTime(format.parse(ary[0]));
                    airRealtimeUserCount.setAreaId(Integer.valueOf(ary[1]));
                    airRealtimeUserCount.setUserCount(Long.parseLong(ary[2]));
                    airRealtimeUserCounts.add(airRealtimeUserCount);
                }
            }
            br.close();
        }
        if(airRealtimeUserCounts != null && airRealtimeUserCounts.size() > 0){
            backService.batchInsertRealtimeUserCount(airRealtimeUserCounts);
        }
        if(airUserFlowCounts != null && airRealtimeUserCounts.size() > 0){
            backService.batchInsertUserFlowCount(airUserFlowCounts);
        }
        sftp.logout();
        return JsonResult.success();
    }
}
